package com.think.teaching.controller;


import com.think.common.controller.BaseController;
import com.think.common.model.Result;
import com.think.common.model.ResultCode;
import com.think.teaching.model.ChapterAssets;
import com.think.teaching.service.impl.ChapterAssetsServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * <p>
 * 课件 前端控制器
 * </p>
 *
 * @author think
 * @since 2020-08-12
 */
@RestController
@RequestMapping("/v1")
@Api(tags =  "课件接口文档")
public class ChapterAssetsController extends BaseController {
	
	public String docReg = ".+(.doc|.docx|.pdf|.txt|.xls|.xlsx)$";
	public String videoReg = ".+(.MP4|.mp4|.mov|.avi|.mkv)$";
	private final ChapterAssetsServiceImpl chapterAssetsService;

	public ChapterAssetsController(ChapterAssetsServiceImpl chapterAssetsService) {
		this.chapterAssetsService = chapterAssetsService;
	}

	@ApiOperation("添加新的课件")
	@PostMapping("admin/chapterAssets")
	public Result AddNewChapterAssets(@RequestBody()ChapterAssets data) {
		Result result = data.CheckObjectComplete();
		if (data.getType() == ChapterAssets.DOCUMENT_ASSET) {
			Matcher matcher =Pattern.compile(docReg).matcher(data.getAsset());
			if (!matcher.find()) {
				return Result.failure(ResultCode.DATA_IS_WRONG);
			}
		}
		else {
			Matcher matcher =Pattern.compile(videoReg).matcher(data.getAsset());
			if (!matcher.find()) {
				return Result.failure(ResultCode.DATA_IS_WRONG);
			}
		}
		if(result.getCode() == ResultCode.SUCCESS.code()) {
			ChapterAssets chapterAssets = chapterAssetsService.AddChapterAssets(data);
			if( chapterAssets!= null) {
				return Result.success(chapterAssets);
			}
			else {
				return Result.failure(ResultCode.SYSTEM_DATABASE_ERROR);
			}
		}
		else
			return result;
	}
	

	@ApiOperation("删除课件")
	@RequestMapping(value = "admin/chapterAssets/{id}",method = RequestMethod.DELETE)
	public Result DeleteChapterAssets(@PathVariable("id")Integer id) {
		ChapterAssets chapterAssets = chapterAssetsService.getById(id);
		if( chapterAssets== null) {
			return Result.failure(ResultCode.CHAPTER_ASSETS_NOT_EXIST);
		}
		else {
			if (chapterAssetsService.DeleteObjectById(id)) {
				return Result.success();
			}
			return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
		}
	}
	
	@ApiOperation("管理员获取课件详情")
	@RequestMapping(value = "admin/chapterAssets/{id}",method = RequestMethod.GET)
	public Result GetChapterAssets(@PathVariable("id")Integer id) {
		ChapterAssets chapterAssets = chapterAssetsService.getById(id);
		if( chapterAssets== null) {
			return Result.failure(ResultCode.CHAPTER_ASSETS_NOT_EXIST);
		}
		else {
			
			return Result.success(chapterAssets);
		}
	}
	
	@ApiOperation("管理员修改课件状态")
	@RequestMapping(value = "admin/chapterAssets/{id}/{state}",method = RequestMethod.PUT)
	public Result UpdateChapterAssetsState(@PathVariable("id")Integer id,@PathVariable("state")Integer state) {
		ChapterAssets chapterAssets = chapterAssetsService.getById(id);
		if( chapterAssets== null) {
			return Result.failure(ResultCode.CHAPTER_ASSETS_NOT_EXIST);
		}
		else {
			try {
				chapterAssets = chapterAssetsService.SetChapterAssetsState(id, state);
				return Result.success(chapterAssets);
			} catch (Exception e) {
				return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
			}
		}
	}
	
	@ApiOperation("用户搜索课件")
	@RequestMapping(value = "user/chapterAssets/search",method = RequestMethod.GET)
	public Result UserSearchChapterAssets(@RequestParam("key")String key) {
		return Result.success(chapterAssetsService.SearchChapterAssetsWithKey(key, true));
	}
	
	@ApiOperation("管理员搜索课件")
	@RequestMapping(value = "admin/chapterAssets/search",method = RequestMethod.GET)
	public Result AdminSearchChapterAssets(@RequestParam("key")String key) {
		return Result.success(chapterAssetsService.SearchChapterAssetsWithKey(key, false));
	}
	
	@ApiOperation("管理员获取课件列表")
	@RequestMapping(value = "admin/chapterAssets/{page}/{count}",method = RequestMethod.GET)
	public Result AdminListChapterAssets(
			@PathVariable("page")Integer page,
			@PathVariable("count")Integer count,
			@RequestParam(required = false) Integer courseType,
			@RequestParam(required = false) Integer type) {
		return Result.success(chapterAssetsService.ListChapterAssetsWithTagAndType(count, page, courseType,  type,false));
	}
	
	@ApiOperation("用户获取课件列表")
	@RequestMapping(value = "user/chapterAssets/{page}/{count}",method = RequestMethod.GET)
	public Result UserListChapterAssets(
			@PathVariable("page")Integer page,
			@PathVariable("count")Integer count,
			@RequestParam(required = false) Integer courseType,
			@RequestParam(required = false) Integer type) {
		return Result.success(chapterAssetsService.ListChapterAssetsWithTagAndType(count, page,  courseType, type,true));
	}
}

